const { ListToNode, NodeToList, ListNode, timeL } = require("../utils.js");

var rotateRight = function (head, k) {
  let nodeHead = head;
  let list = [];
  while (head) {
    list.push(head);
    head = head.next;
  }
  let leng = list.length;
  k = k % leng;
  if (!k) return nodeHead;
  let resHead = list[leng - k];
  let res = resHead;
  for (let i = leng - k + 1; i < leng; i++) {
    res.next = list[i];
    res = res.next;
  }
  for (let i = 0; i < leng - k; i++) {
    res.next = list[i];
    res = res.next;
  }
  res.next = null;
  return resHead
};
let head = [1, 2, 3, 4, 5],
  k = 2;

timeL(rotateRight, [ListToNode(head), k]);
